﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class CarScrapDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CarScrap model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CarScrap(");
            strSql.Append("CarInfoId,ScrapTime,RegistrationTime,ServiceLife,ScrapAddress,ResidualValue,ResidualAmount,ScrapReson,UserId)");
            strSql.Append(" values (");
            strSql.Append("@CarInfoId,@ScrapTime,@RegistrationTime,@ServiceLife,@ScrapAddress,@ResidualValue,@ResidualAmount,@ScrapReson,@UserId)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@CarInfoId", SqlDbType.Int,4),
					new SqlParameter("@ScrapTime", SqlDbType.DateTime),
					new SqlParameter("@RegistrationTime", SqlDbType.DateTime),
					new SqlParameter("@ServiceLife", SqlDbType.Int,4),
					new SqlParameter("@ScrapAddress", SqlDbType.NVarChar,100),
					new SqlParameter("@ResidualValue", SqlDbType.Decimal),
					new SqlParameter("@ResidualAmount", SqlDbType.Decimal),
					new SqlParameter("@ScrapReson", SqlDbType.NVarChar),
					new SqlParameter("@UserId", SqlDbType.Int,4)};
            parameters[0].Value = model.CarInfoId;
            parameters[1].Value = model.ScrapTime;
            parameters[2].Value = model.RegistrationTime;
            parameters[3].Value = model.ServiceLife;
            parameters[4].Value = model.ScrapAddress;
            parameters[5].Value = model.ResidualValue;
            parameters[6].Value = model.ResidualAmount;
            parameters[7].Value = model.ScrapReson;
            parameters[8].Value = model.UserId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CarScrap model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CarScrap set ");
            strSql.Append("CarInfoId=@CarInfoId,");
            strSql.Append("ScrapTime=@ScrapTime,");
            strSql.Append("RegistrationTime=@RegistrationTime,");
            strSql.Append("ServiceLife=@ServiceLife,");
            strSql.Append("ScrapAddress=@ScrapAddress,");
            strSql.Append("ResidualValue=@ResidualValue,");
            strSql.Append("ResidualAmount=@ResidualAmount,");
            strSql.Append("ScrapReson=@ScrapReson,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("UserId=@UserId");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@CarInfoId", SqlDbType.Int,4),
					new SqlParameter("@ScrapTime", SqlDbType.DateTime),
					new SqlParameter("@RegistrationTime", SqlDbType.DateTime),
					new SqlParameter("@ServiceLife", SqlDbType.Int,4),
					new SqlParameter("@ScrapAddress", SqlDbType.NVarChar,100),
					new SqlParameter("@ResidualValue", SqlDbType.Decimal),
					new SqlParameter("@ResidualAmount", SqlDbType.Decimal),
					new SqlParameter("@ScrapReson", SqlDbType.NVarChar),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.CarInfoId;
            parameters[1].Value = model.ScrapTime;
            parameters[2].Value = model.RegistrationTime;
            parameters[3].Value = model.ServiceLife;
            parameters[4].Value = model.ScrapAddress;
            parameters[5].Value = model.ResidualValue;
            parameters[6].Value = model.ResidualAmount;
            parameters[7].Value = model.ScrapReson;
            parameters[8].Value = model.CreateTime;
            parameters[9].Value = model.UserId;
            parameters[10].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CarScrap ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idList)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CarScrap ");
            strSql.Append(" where Id in (" + idList + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 根据条件获取前几条数据
        /// </summary>
        /// <param name="strWhere">The Query String.</param>
        /// <returns>RepairManual List</returns>
        public IList<CarScrap> GetList(string strWhere)
        {
            IList<CarScrap> result = new List<CarScrap>();
            string sql = "SELECT * FROM Vw_CarScrap WHERE 1=1" + strWhere;
            using (SqlDataReader dr = DbHelperSQL.ExecuteReader(sql))
            {
                while (dr.Read())
                {
                    try
                    {
                        CarScrap obj = new CarScrap();
                        obj.Id = (dr["Id"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("Id")));
                        obj.CarInfoId = (dr["CarInfoId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("CarInfoId")));
                        obj.ScrapTime = (dr["ScrapTime"] == System.DBNull.Value ? new DateTime(1900, 1, 1) : dr.GetDateTime(dr.GetOrdinal("ScrapTime")));
                        obj.RegistrationTime = (dr["RegistrationTime"] == System.DBNull.Value ? new DateTime(1900, 1, 1) : dr.GetDateTime(dr.GetOrdinal("RegistrationTime")));
                        obj.ServiceLife = (dr["ServiceLife"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("ServiceLife")));
                        obj.ScrapAddress = (dr["ScrapAddress"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("ScrapAddress")));
                        obj.ResidualValue = (dr["ResidualValue"] == System.DBNull.Value ? 0M : dr.GetDecimal(dr.GetOrdinal("ResidualValue")));
                        obj.ResidualAmount = (dr["ResidualAmount"] == System.DBNull.Value ? 0M : dr.GetDecimal(dr.GetOrdinal("ResidualAmount")));
                        obj.ScrapReson = (dr["ScrapReson"] == System.DBNull.Value ? string.Empty : dr.GetString(dr.GetOrdinal("ScrapReson")));
                        obj.CreateTime = (dr["CreateTime"] == System.DBNull.Value ? new DateTime(1900, 1, 1) : dr.GetDateTime(dr.GetOrdinal("CreateTime")));
                        obj.UserId = (dr["UserId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("UserId")));
                        obj.CarNo = (dr["CarNo"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("CarNo")));
                        result.Add(obj);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("从DataReader中读取数据失败，可能与相关的字段不存在有关！请检查！||" + ex.Message + "||" + ex.Source);
                    }
                }
                dr.Close();
            }
            return result;
        }
        #endregion  BasicMethod
    }
}
